<template>
<el-container>
  <el-header style="background-color: white; color: black;">
      <h1 align="center">用户操作</h1>
  </el-header>
  <el-main>
    <!-- 查询条件表单 -->
    <el-form :inline="true" :model="queryForm" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="queryForm.userName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="queryForm.userTel" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="queryForm.userEmail" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-select v-model="queryForm.userRole" placeholder="权限">
          <el-option label="普通管理员" value="admin"></el-option>
          <el-option label="超级管理员" value="sadmin"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
        <el-button type="primary" @click="addUser()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="100%">
      <!-- 列组件 -->
      <!-- prop 对应当前页集合的对象属性-->
      <el-table-column prop="userId" label="编号" style="100%"></el-table-column>
      <el-table-column prop="userName" label="姓名" style="100%"></el-table-column>
      <el-table-column prop="userEmail" label="邮箱" style="100%"></el-table-column>
      <el-table-column prop="userTel" label="电话" style="100%"></el-table-column>
      <el-table-column prop="userRole" label="权限" style="100%"></el-table-column>
      <el-table-column label="操作" width="200">
          <!-- slot-scope 插槽: 用来获取循环的迭代变量-->
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="update(scope.row,scope.$index)">修改</el-button>
            <el-button type="danger" size="mini" @click="deleteUser(scope.row,scope.$index)">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
    @size-change="select"
    @current-change="selectPage"
    :current-page="queryForm.page"
    :page-sizes="[10,15,20]"
    :page-size="queryForm.row"
    :total="total"
    layout="total,sizes,prev,pager,next,jumper"
    >
    </el-pagination>
    <el-dialog :title="title" :visible.sync="isShow" width="50%">
        <el-form :model="userForm" status-icon :rules="rules" ref="userForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="姓名" v-show="userForm.isShow" prop="userName">
                <el-input v-model="userForm.userName" ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="userPwd">
                <el-input type="password" v-model="userForm.userPwd" ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="ensurePwd">
                <el-input type="password" v-model="userForm.ensurePwd" ></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="userEmail"
                :rules="[
                  { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                  { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                ]"
            >
              <el-input style="width: 300px;" v-model="userForm.userEmail"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="userTel">
              <el-input v-model="userForm.userTel"></el-input>
            </el-form-item>
            <el-form-item label="权限">
              <el-select v-model="userForm.userRole" placeholder="权限">
                <el-option label="普通管理员" value="admin"></el-option>
                <el-option label="超级管理员" value="sadmin"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="submitForm(index)">提交</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
  </el-main>
</el-container>
</template>

<script>
  export default {
      data() {
        var validatePass = (rule, value, callback) => {
                console.log("v="+value)
                if (value === '' || value==undefined) {
                  callback(new Error('请输入密码'));
                } else {
                  if (this.userForm.ensurePwd !== '') {
                    this.$refs.userForm.validateField('ensurePwd');
                  }
                  callback();
                }
              };
        var validatePass2 = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请再次输入密码'));
          } else if (value !== this.userForm.userPwd) {
            callback(new Error('两次输入密码不一致!'));
          } else {
            callback();
          }
        };
        return {
          isShow:false,
          tableData: [],
          totalPage: 0, //总页数
          total: 0, //总条数
          title:"修改",
          index:0 ,  //0为修改  1为添加
          queryForm: {
            page: 1,
            row: 10,
            userName:"",
            userTel:"",
            userEmail:"",
            userRole:"",
          },
          userForm: {
            isShow:false,
            userId:0,
            userName:"",
            userPwd:"",
            userTel:"",
            userEmail:"",
            userRole:"",
            ensurePwd:"",
          },
          rules: {
            userPwd: [
              { validator: validatePass, trigger: 'blur' }
            ],
            ensurePwd: [
              { validator: validatePass2, trigger: 'blur' }
            ]
        },
        }
      },
      methods: {
        loadData() { //加载数据
          console.log(this.queryForm)
          var self = this;
          var http = this.$http.post("/api/user/userIndex",this.$qs.stringify(this.queryForm));
          http.then(function(rs) {
            if (rs.data.code == 200) {
              self.tableData = rs.data.data.data;
              self.total = rs.data.data.total;
              self.totalPage = rs.data.data.totalPage;
            }else{
              self.tableData=[];
              self.total=0;
            }
          })
        },
        select(row){ //分页下拉框选中触发的事件，row是当前选中条数
          this.queryForm.row=row;
          //刷新
          this.loadData();
        },
        selectPage(page){//选中页码触发的事件, page就是选中的页码
          this.queryForm.page = page;
          //刷新
          this.loadData();
        },
        update(x,index) { //修改回显
          this.title = "修改"
          this.userForm.ensurePwd = "";
          this.userForm = x;
          this.isShow = true;
          this.userForm.userPwd = "";
          this.index = 0
        },
        addUser(){
          this.title = "添加"
          this.isShow = true;
          this.userForm.ensurePwd="";
          this.userForm = {};
          this.userForm.isShow = true;
          this.userForm.userId = 0;
          this.index = 1;
        },
        deleteUser(x,index) { //删除
          this.userForm = x;
          var self = this
          var http = this.$http.post("api/user/deleteUser",this.$qs.stringify(this.userForm));
          http.then(function(rs){
              if(rs.data.code == 200){
                alert(rs.data.msg)
                location.reload();
              }
          })
        },
        submitForm(i){
          if(i == 0){
            var self = this
            var http = this.$http.post("api/user/updateOtherUser",this.$qs.stringify(this.userForm));
            http.then(function(rs){
                if(rs.data.code == 200){
                  alert(rs.data.msg)
                  location.reload();
                }else{
                  alert(rs.data.msg)
                }
            })
          }else{
            var self = this
            var http = this.$http.post("api/user/addUser",this.$qs.stringify(this.userForm));
            http.then(function(rs){
                if(rs.data.code == 200){
                  alert(rs.data.msg)
                  location.reload();
                }else{
                  alert(rs.data.msg)
                }
            })
          }
        }
      },
      mounted(){
        this.loadData();
      },
    }
</script>

<style>
  .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }
</style>
